package com.nervenets.shopping.hibernate.domain.order;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.shopping.hibernate.domain.shopping.Product;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class GroupBuyOrder extends DomainObject {
    @ApiModelProperty(value = "商品")
    @ManyToOne
    @JoinColumn(columnDefinition = "product_id")
    @JsonIgnore
    @JSONField(serialize = false)
    private Product product;
    @ApiModelProperty(value = "成团数量")
    private int successCount;
    @ApiModelProperty(value = "拼团失败奖励金额")
    private int failedAward;
    @ApiModelProperty(value = "参与人员")
    @JsonIgnore
    @JSONField(serialize = false)
    @OneToMany(cascade = CascadeType.ALL)
    @OrderBy(value = "createTime")
    @JoinColumn(name = "order_id")
    @Builder.Default
    private List<GroupBuyOrderUser> users = new ArrayList<>();
    @ApiModelProperty(value = "成团时间")
    private long successTime;
    @ApiModelProperty(value = "处理时间")
    private long handleTime;
    @ApiModelProperty(value = "拼团公益金额")
    private long contribution;
    @ApiModelProperty(value = "拼团分红金额")
    private long bonus;

    public long getProductId() {
        return this.product.getId();
    }

    public String getProductName() {
        return this.product.getName();
    }

    public boolean isGrouped() {
        return successCount == this.users.size();
    }

    public int getJoinUserCount() {
        return null != this.users ? this.users.size() : 0;
    }
}
